import {
  dealSuggest
} from '../../../services/api/order'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsListLoadStatus: 0,
    pageLoading: false,
    orderList: [],
    customerId: "",
    orderId: '',
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    customerName: "",
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  submit() {
    this.loadGoodsList(true)
  },
  clearContent() {
    this.setData({
      customerName: ""
    }, () => {
      this.loadGoodsList(true)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查
    let orderId = options.orderId;
    let customerId = options.customerId;
    this.setData({
      orderId,
      customerId
    })
    this.initData()
  },
  navDetail(e) {
    console.log(e);
    wx.navigateTo({
      url: `/subpages/pages/addSaleOrder/addSaleOrder?isEdit=true&kid=${e.currentTarget.dataset.group.kid}`,
    })
  },


  async initData() {
    let {
      data,
      code
    } = await dealSuggest({
      // "endTime": "string",
      "pageNo": 0,
      "pageSize": 10,
      "param": {
        "agentUserId": this.data.orderId,
        customerId: this.data.customerId
        // "customerId": "string",
        // "customerName": "string",
        // "customerPhone": "string",
        // "empUserId": "string"
      },
      // "startTime": "string",
      // "timeRange": "string"
    })

    if (data.entities) {
      this.setData({
        orderList: data.entities
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  handleClick() {
    wx.navigateTo({
      url: `/subpages/pages/addSaleOrder/addSaleOrder?orderId=${this.data.orderId}&customerId=${this.data.customerId}`,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onShow() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await dealSuggest({
        "pageNo": pageIndex,
        "pageSize": pageSize,
        "param": {
          "agentUserId": this.data.orderId,
          customerId: this.data.customerId,
          customerName: this.data.customerName
        }
      });
      this.setData({
        orderList: fresh ? data.entities : this.data.orderList.concat(data.entities),
        goodsListLoadStatus: data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },
})